package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.dto.OrderPageDTO;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    AddressBookService addressBookService;

    @Autowired
    UserService userService;

    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    OrderDetailService orderDetailService;



    @Override
    public R<String> payment(Orders order) {

        AddressBook address = addressBookService.getById(order.getAddressBookId());
        Long UserId = BaseContext.getCurrentId();
        User user = userService.getById(UserId);
        if (ObjectUtils.isEmpty(address)) {
            return R.error("地址不能为空!");
        }


        order.setUserId(UserId);
        order.setStatus(1);
        order.setOrderTime(LocalDateTime.now());
        order.setUserName(user.getName());
        order.setPhone(user.getPhone());
        order.setAddress(address.getDetail());
        long id = IdWorker.getId();
        //order.setId(id);
        order.setNumber(id + "");
        String consignee = address.getConsignee();
        order.setConsignee(consignee);


        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, UserId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        BigDecimal sum = new BigDecimal(0);
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart shoppingCart : list) {
            BigDecimal amount = shoppingCart.getAmount();
            Integer number = shoppingCart.getNumber();
            BigDecimal totalPrice = amount.multiply(new BigDecimal(number));
            sum = sum.add(totalPrice);
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            //orderDetail.setOrderId(order.getId());
            //orderDetail.setOrderId(id);
            orderDetails.add(orderDetail);
        }
        order.setAmount(sum);

        this.save(order);
        Long id1 = order.getId();
        for (OrderDetail orderDetail : orderDetails) {
            orderDetail.setOrderId(id1);
        }
        orderDetailService.saveBatch(orderDetails);


        shoppingCartService.remove(wrapper);
        return R.success("成功!");
    }


    @Override
    public R<Page<OrderPageDTO>> userPage(Long page, Long pageSize) {
        if (ObjectUtils.isEmpty(page)) {
            page = 1L;
        }
        if (ObjectUtils.isEmpty(pageSize)) {
            pageSize = 1L;
        }


        Page<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderTime);
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        Page<Orders> page1 = this.page(orderPage, wrapper);
        List<Orders> records = page1.getRecords();
        ArrayList<OrderPageDTO> orderPageDTOS = new ArrayList<>();
        for (Orders record : records) {
            OrderPageDTO orderPageDTO = new OrderPageDTO();
            Long id = record.getId();
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> list = orderDetailService.list(wrapper1);
            BeanUtils.copyProperties(record, orderPageDTO);
            orderPageDTO.setOrderDetails(list);
            orderPageDTOS.add(orderPageDTO);
        }
        Page<OrderPageDTO> orderPageDTOPage = new Page<>();
        BeanUtils.copyProperties(page1, orderPageDTOPage);
        orderPageDTOPage.setRecords(orderPageDTOS);
        return R.success(orderPageDTOPage);
    }

    @Override
    public R<Page<Orders>> orderPage(Long page, Long pageSize, String number, Date beginTime, Date endTime) {
        if (ObjectUtils.isEmpty(page)) {
            page = 1L;
        }
        if (ObjectUtils.isEmpty(page)) {
            page = 10L;
        }


        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtils.isEmpty(number), Orders::getNumber, number);
        wrapper.between(!ObjectUtils.isEmpty(beginTime) && !ObjectUtils.isEmpty(endTime), Orders::getOrderTime, beginTime, endTime);
        Page<Orders> page1 = this.page(ordersPage, wrapper);

        return R.success(page1);
    }
}